import {
    ArgumentsHost,
    ExceptionFilter,
    HttpException,
    HttpStatus,
} from '@nestjs/common';
import { Response, Request } from 'express';

export class HttpFilter implements ExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();
        const statusCode =
            exception instanceof HttpException
                ? exception.getStatus()
                : HttpStatus.INTERNAL_SERVER_ERROR;
        response.status(200).json({ //异常捕获并返回正常给前端
            statusCode,
            data: null,
            message: exception?.message ?? 'Internal server error',
            timestamp: new Date().toISOString(),
            path: request.url,
        });
    }
}

